package ext.tianma.change.processors;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import wt.change2.WTChangeOrder2;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;

public class DelectModifiedFinishedGoodTableProcessors extends DefaultObjectFormProcessor{

	@Override
	public FormResult doOperation(NmCommandBean nmcommandbean, List list)
			throws WTException {
		
		FormResult formResult = super.doOperation(nmcommandbean, list);
		System.out.println( "Debug    进入方法体!" );
		
		NmOid nmoid = nmcommandbean.getActionOid(); 
		WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
		WTChangeOrder2 ecn = null;
		if( ( contextObj != null ) && ( contextObj instanceof WTChangeOrder2 ) ){
			ecn = ( WTChangeOrder2 )contextObj;
		}
		
		Map<String, String[]> map = nmcommandbean.getRequest().getParameterMap();
		Set<String> set = map.keySet();
		Iterator<String>  iterator = set.iterator();
		
		while ( (iterator != null) && ( iterator.hasNext() ) ) {
			String key = iterator.next();
			
			if( ( key != null ) && ( key.equals( "pjl_selPJLsa1__1" ) )){
				
				String[]  mapValue =map.get(key);
				if( ( mapValue != null ) && ( mapValue.length>0 ) ){
					String ibaValue = getIBAValue( ecn );
					
					
					for (int i = 0; i < mapValue.length; i++) {
						String value = mapValue[i];
						value = ModifiedFinishedGoodUtil.getValue(value);
						ibaValue = updateIBAValue( value , ibaValue );
					}
					
					setECNIBAValue(ecn,ibaValue);
					
				}
				
			}
			
		}
		
		return formResult;
	}
	
	/**
	 * 获取ecn上的IBA属性值
	 * @param ecn
	 * @return
	 */
	private static String getIBAValue( WTChangeOrder2 ecn ){
		
		String ibaValue = "";
		if( ecn != null ){
			try {
				
				ibaValue = (String)IBAUtil.getIBAValue(ecn, "modifiedFinishedGood");
				
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		return ibaValue;
	}
	
	/**
	 * 向ECN的IBA属性上更新属性值
	 * @param ecn
	 * @param ibaValue
	 */
	private static void setECNIBAValue(WTChangeOrder2 ecn, String ibaValue) {
		
		if( ( ecn != null ) && ( ibaValue != null ) ){
			try {
				
				IBAUtil.forceSetIBAValue(ecn, "modifiedFinishedGood", ibaValue);
				
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 
	 * @param value
	 * @param ibaValue
	 * @return
	 */
	private static String updateIBAValue( String value ,String ibaValue){
		
		String ecnvalue = "";
		if( ( value != null ) ){
			
			try {
				
				WTObject wtObject =(WTObject) CoreUtil.getWTObjectByOid( value );
				if( ( wtObject != null ) && ( wtObject instanceof WTPart ) ){
					WTPart part = ( WTPart )wtObject;
					ecnvalue = ModifiedFinishedGoodUtil.getDelectIBAValue(part, ibaValue);
				}
				
			} catch (WTException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
		}
		return ecnvalue;
		
	}

}
